<template>
  <div>
    <Form :model="approvalForm" :rules="ruleValidate" ref="form" :label-width="130">
      <Form-item :label="'是否添加审批人:'" prop="isApprove">
        <RadioGroup v-model="approvalForm.isApprove">
          <Radio label="1" :disabled="submitParams.isApprove==='0'">是</Radio>
          <Radio label="0" :disabled="submitParams.isApprove==='1'">否</Radio>
        </RadioGroup>
      </Form-item>
      <Form-item :label="'选择审批人:'" key="approverList" prop="approverList" v-if="approvalForm.isApprove === '1'">
        <Tag
          type="border"
          v-for="item in approvalForm.approverList"
          :key="item.id"
          :name="item"
          :closable="formId ? item.id == formId : true"
          @on-close="handleClose"
          >{{ item.userName }}</Tag
        >
        <RPButton
          :disabled="!!submitParams.processId"
          type="button"
          optType="create"
          size="small"
          @click="handleSelectUser"
          text="添加审批人"
          html-type="button"
          icon="md-add"
        />
      </Form-item>
      <Form-item v-if="approvalForm.isApprove === '1'" :label="'意见:'" key="commentMsg" prop="commentMsg">
        <Input
          type="textarea"
          maxlength="300"
          :rows="4"
          v-model="approvalForm.commentMsg"
          placeholder="请输入,限制300字"
        ></Input>
      </Form-item>
    </Form>
  </div>
</template>

<script>
import SelectUserPage from '../../../../bpmCenter/components/SelectUser.vue'
import formCenterApi from '@/api/formCenter.js'
export default {
  data() {
    return {
      approvalForm: {
        isApprove: '1',
        approverList: [],
        commentMsg: ''
      },
      ruleValidate: {
        isApprove: [{ required: true, message: '请选择是否需要审批', trigger: 'change' }],
        approverList: [{ required: true, type: 'array', message: '请选择审批人', trigger: 'change' }]
      }
    }
  },
  props: {
    submitParams: {
      type: Object,
      default: () => {}
    }
  },
  methods: {
    handleSelectUser() {
      new this.$pageModal(
        SelectUserPage,
        {
          props: {}
        },
        data => {
          if (data) {
            data.forEach(element => {
              this.approvalForm.approverList.push(element)
            })
            this.approvalForm.approverList = this.uniqueByKey(this.approvalForm.approverList, 'userId')
          }
        }
      )
    },
    uniqueByKey(arr, key) {
      const seen = new Set()
      return arr.filter(item => {
        const serializedKey = JSON.stringify(item[key])
        return seen.has(serializedKey) ? false : seen.add(serializedKey)
      })
    },
    handleClose(event, form) {
      if (this.submitParams.processId) {
        return
      }
      let index = this.approvalForm.approverList.indexOf(form)
      if (index > -1) {
        this.approvalForm.approverList.splice(index, 1)
      }
    }
  },
  created() {
    console.log("----", this.submitParams, JSON.parse(this.submitParams.config))
    this.$emit('SetTitle', '制表确认')
    this.$emit('SetPageWidth', 700)
    this.$emit('SetPageMinHeight', 200)
    this.$emit('SetPageActions', [
      {
        text: '取消',
        handle: () => {
          this.$emit('Close')
        }
      },
      {
        text: '确定',
        theme: 'primary',
        handle: () => {
          let host = location.host
          if (host.includes('sit')) {
            host = 'crinfo-uat.test.crcloud.com'
          }
          let pcUrl = `https://${host}/form.html?type=template&formType=${this.submitParams.type}&id=${this.submitParams.id}&recordId=`
          const params = {
            ...this.submitParams,
            ...this.approvalForm,
            pcUrl,
            isApprove: this.approvalForm.isApprove === '1' ? true : false,
            approverList: this.approvalForm.approverList
              ? this.approvalForm.approverList.map(item => item.userId).join(' ')
              : ''
          }
          const config = JSON.parse(this.submitParams.config)
          // 判断treeData是否存在指标值，若是空的不让提交
          const isNoEmpty = config.treeData[0]?.children?.length > 0
          // 存储是否需要审批和审批人到baseInfo中，用于驳回时使用
          config.approverList = this.approvalForm.approverList
          config.isApprove = this.approvalForm.isApprove
          params.config = JSON.stringify(config)
          this.$refs['form'].validate(valid => {
            if (valid) {
              if (!isNoEmpty) {
                this.$Message.error({
                  content: '请先添加指标值',
                  duration: 2
                })
                return
              }
              formCenterApi.formSubmit(params).then(res => {
                if (res.data.code === 200) {
                  this.$emit('Close', true)
                }
              })
            }
          })
        }
      }
    ])
    if (this.submitParams.processId) {
      this.approvalForm.isApprove = this.submitParams.isApprove
      this.approvalForm.approverList = this.submitParams.approverList || []
    }
  }
}
</script>

<style lang="scss" scoped></style>
